/**
 * 2007-4-18 12:19:09
 */
package org.lightpersist.dao;

import java.util.List;

/**
 * @author <a href="mailto:gl@kindsoft.cn">Jason Green</a>
 * @see Page
 */
public class Page<T> {

	private int currentPage;

	private List<T> items;

	private int maxResult;

	private int total;

	private int pagesCount;

	private boolean hasNext;

	private boolean hasPrev;

	public Page(final int currentPage, final int maxResult) {
		super();
		this.currentPage = currentPage;
		this.maxResult = maxResult;
		hasPrev = currentPage > 1;
	}

	public Page(final int currentPage, final int maxResult,
			final List<T> items, final int total) {
		this(currentPage, maxResult);
		setItems(items);
		setTotal(total);
	}

	/**
	 * @param itemsCount
	 *            the itemsCount to set
	 */
	public final void setTotal(final int itemsCount) {
		this.total = itemsCount;
		pagesCount = itemsCount / maxResult;
		if ((itemsCount % maxResult) > 0) {
			pagesCount++;
		}
		hasNext = currentPage < pagesCount;
	}

	/**
	 * @param items
	 *            the items to set
	 */
	public final void setItems(final List<T> items) {
		this.items = items;
	}

	/**
	 * @return the startIndex
	 */
	public int getStartIndex() {
		return (currentPage-1) * maxResult;
	}

	/**
	 * @return the currentPage
	 */
	public final int getCurrentPage() {
		return currentPage;
	}

	/**
	 * @return the hasNextPage
	 */
	public final boolean hasNext() {
		return hasNext;
	}

	/**
	 * @return the hasPrePage
	 */
	public final boolean hasPrev() {
		return hasPrev;
	}

	/**
	 * @return the items
	 */
	public final List<T> getItems() {
		return items;
	}

	/**
	 * @return the itemsCount
	 */
	public final int getTotal() {
		return total;
	}

	/**
	 * @return the maxResult
	 */
	public final int getMaxResult() {
		return maxResult;
	}

	/**
	 * @return the pagesCount
	 */
	public final int getPagesCount() {
		return pagesCount;
	}

}
